package com.servlet.response; /**
 * @author wzm
 * @create 2023-06-25 10:27
 */

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.FileInputStream;
import java.io.IOException;

@WebServlet("/rs04")
public class ResponseServlet04 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //响应字节数据到浏览器
        //响应一个图片到浏览器

        //从磁盘读取文件
        FileInputStream fis = new FileInputStream("f://a.png");

        //response字节输出流 写数据
        ServletOutputStream os = response.getOutputStream();

        //通过循环将输入流中的数据 拿出来 在通过输出流写出到浏览器
        byte[] buff = new byte[1024];
        int len = 0;

        while ((len = fis.read(buff)) != -1){
            System.out.println(len);
            //表示你读到了数据
            //读一点写一点到浏览器
            os.write(buff,0,len);
        }

        //关闭流
        fis.close();

    }
}
